// mycomponent/Waterfalls_flow.js
var location = require("../../mybehaviors/location/location");
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  behaviors: [location],
  options: {
    styleIsolation: 'shared',
  },
  data: {
    showList: false,
    cityName:'',
    cityID:'',
    selectGoodsList:[],
    goodsList:[],
    activeTab:0,
    tabIndexNumber:1,
    searchText:'',
    total:null,
    page:1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    // this.getGoodsList(1,'')
    if(options.ids){
      this.setData({
        'selectGoodsList':options.ids.split(',')
      })
    }
  },
  tolocation() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const url = `/${currentPage.route}?page_id=${this.data.page_id}`;
    wx.setStorageSync('diyurl', url);
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=goback'
    });

  },
  saveBtnClick(){
    let selectGoods = []
    this.data.goodsList.forEach((item)=>{
      if(item.selected){
        selectGoods.push(item)
      }
    })
    wx.setStorageSync('PostGoods', selectGoods)
    wx.navigateBack({
      delta: 1
    })
  },
  cancelBtnClick(){
    this.data.goodsList.forEach((item)=>{
      item.selected = false
    })
    this.setData({
      goodsList:this.data.goodsList
    })
  },
  searchClick(){
    this.getGoodsList(this.data.tabIndexNumber,this.data.searchText)
  },
  goSearch(){
    this.setData({
      goodsList:[],
      page:1
    })
    this.getGoodsList(this.data.tabIndexNumber,this.data.searchText)
  },
  onChange(e){
    this.setData({
      searchText:e.detail
    })
  },
  onSearch(e){
    this.setData({
      goodsList:[],
      searchText:e.detail,
      page:1
    })
    this.getGoodsList(this.data.tabIndexNumber,this.data.searchText)
  },
  changeTab(e){
    let index = e.detail.index+1
    this.setData({
      goodsList:[],
      tabIndexNumber:index,
      page:1
    })
    this.getGoodsList(index,'','hide')
  },
  goodsClick(e){
    let index = e.currentTarget.dataset.index;
    this.data.goodsList.forEach((item)=>{
      item.selected = false
    })
    if(this.data.goodsList[index].selected){
      this.data.goodsList[index].selected = false
    } else {
      this.data.goodsList[index].selected = true
    }
    this.setData({
      goodsList:this.data.goodsList
    })
  },
  getGoodsList(tabIndex,goodsName,status){
    wx.showLoading({
      title: '查询中请稍后...',
    })
    if(status){
      this.setData({
        showList:false
      })
    }
    const _this = this
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.trick.getWishGoods");
    app._postNetWork({
      url: urlStr,
      data: {'city_name':_this.data.cityName,'goods_name':goodsName,'goods_type':tabIndex,'page':_this.data.page},
      success: function(res) {
        const resData = res.data
        console.log(resData,'-------------')
        if (resData.result == 1) {
          resData.data.data.forEach((item)=>{
            item['selected'] = false
            _this.data.selectGoodsList.forEach((ite)=>{
              if(ite == item.id){
                item['selected'] = true
              }
            })
          })
          _this.setData({
            goodsList : _this.data.goodsList.concat(resData.data.data),
            total:resData.data.total
          })
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
        if(status){
          _this.setData({
            showList:true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      showList: false,
      goodsList:[],
      page:1
    })
    this._getLocation((mapdata,e)=>{
      console.log(mapdata,'----------mapdata')
      this.setData({
        cityName: mapdata.address_component.city ? mapdata.address_component.city : mapdata.address_component.province,
        cityID:mapdata.ad_info.city_code
      });
      if(this.data.tabIndexNumber){
        this.getGoodsList(this.data.tabIndexNumber,'','hide')
      } else {
        this.getGoodsList(1,'','hide')
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.goodsList.length != this.data.total){
      this.setData({
        page: this.data.page+=1
      })
      this.getGoodsList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})